package org.jeecg.modules.wms.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.wms.goods.entity.WmsCargoOwners;
import org.jeecg.modules.wms.goods.entity.WmsProducts;
import org.jeecg.modules.wms.goods.mapper.WmsProductsMapper;
import org.jeecg.modules.wms.goods.service.IWmsCargoOwnersService;
import org.jeecg.modules.wms.goods.service.IWmsProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 商品信息表
 * @Author: jeecg-boot
 * @Date:   2025-09-03
 * @Version: V1.0
 */
@Service
public class WmsProductsServiceImpl extends ServiceImpl<WmsProductsMapper, WmsProducts> implements IWmsProductsService {
@Autowired
private WmsProductsMapper wmsProductsMapper;
@Autowired
private RedisUtil redisUtil;
    @Override
    public IPage<WmsProducts> queryList(WmsProducts wmsProducts, Integer pageNo, Integer pageSize) {
        //进行分页查询
        Page<WmsProducts> page = PageHelper.startPage(pageNo, pageSize);
        //查询数据
        List<WmsProducts> list = wmsProductsMapper.queryList(wmsProducts);
        PageDTO<WmsProducts> pageVo = new PageDTO<>();
        pageVo.setRecords(list);  // 数据
        pageVo.setTotal(page.getTotal()); // 总数
        pageVo.setPages(page.getPages()); // 总页数
        pageVo.setCurrent(pageNo); // 当前页
        pageVo.setSize(pageSize); // 每页数
        return pageVo;
    }

    /**
     * 添加商品
     * @param wmsProducts
     */
    @Autowired
    private IWmsCargoOwnersService wmsCargoOwnersService;
    @Override
    public void add(WmsProducts wmsProducts) {
        //设置商品条码，规则6位货主编码+4位商品类别+6位序号
        //货主编码
        String ownerCode = wmsCargoOwnersService.getById(wmsProducts.getOwnerId()).getOwnerCode();
        //截取货主编码前6位
        ownerCode = ownerCode.substring(0, 6);
        //获取商品类型编码
        String categoryCode = wmsProducts.getCategoryId();
        //截取商品类型编码前4位
        categoryCode = categoryCode.substring(0, 4);
        String code = generatedCode();
        wmsProducts.setProductBarcode(ownerCode + categoryCode + code);
        save(wmsProducts);

    }



    /**
     * 生产6位序号
     * @return
     */
    public String generatedCode() {

        long incr = redisUtil.incr("Product_Barcode", 1);
        String incrStr = String.format("%06d", incr);
        return incrStr;
    }


}
